import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-06-23
 * Time: 15:24
 */
public class MyArrayList<E> implements IOperation<E> {

        public Object[] elem;
        public int usedSize;//0
        //默认容量
        private static final int DEFAULT_SIZE = 2;

        public MyArrayList() {
            this.elem = new Object[DEFAULT_SIZE];
        }

    @Override
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.println(this.elem[i]);
        }
    }

    @Override
    public void add(E data) {
        isFull();
        this.elem[this.usedSize] = data;
        this.usedSize++;
    }

    @Override
    public void isFull() {
        if(this.usedSize == this.elem.length) {
            this.elem = Arrays.copyOf(elem,this.usedSize*2);
        }
    }

    @Override
    public void add(int pos, E data) throws IndexErrorException{
        isFull();
        try {
            if(isAddIndexError(pos)) {
                throw new IndexErrorException("下标不合法！！！");
            }
            for (int i = usedSize - 1; i >= pos; i--) {
                elem[i+1] = elem[i];
            }
            elem[pos] = data;
            this.usedSize++;
        }catch (IndexErrorException e) {
            e.printStackTrace();
        }
    }

    private boolean isAddIndexError(int pos) {
            if(pos < 0 || pos > usedSize) {
                return true;
            }
            return false;
    }

    @Override
    public boolean contains(E toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(elem[i].equals(toFind)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(E toFind) {
        for (int i = 0; i < this.usedSize; i++) {
            if(elem[i].equals(toFind)) {
                return i;
            }
        }
        return -1;
    }

    private boolean isEmpty() {
        if (this.usedSize == 0) {
            return true;
        }
        return false;
    }
    @Override
    public E get(int pos) throws EmptyException,IndexErrorException{
        try {
            if(isEmpty()) {
                throw new EmptyException("顺序表为空！！！");
            }
            if(isGetIndexError(pos)) {
                throw new IndexErrorException("下标不合法！！！");
            }
            return (E)elem[pos];
        }catch (IndexErrorException e) {
            e.printStackTrace();
        }catch (EmptyException e) {
            e.printStackTrace();
        }
        return null;
    }

    private boolean isGetIndexError(int pos) {
        if(pos < 0 || pos >= this.usedSize) {
            return true;
        }
        return false;
    }

    @Override
    public void set(int pos, E value) {
            try {
                if(isEmpty()) {
                    throw new EmptyException("顺序表为空！！！");
                }
                if(isSetIndexError(pos)) {
                    throw new IndexErrorException("下标不合法！！！");
                }
                elem[pos] = value;
            }catch (IndexErrorException e) {
                e.printStackTrace();
            }catch (EmptyException e) {
                e.printStackTrace();
            }
    }

    private boolean isSetIndexError(int pos) {
        if(pos < 0 || pos >= usedSize) {
            return true;
        }
        return false;
    }
    @Override
    public void remove(E key) {
           try {
               if(isEmpty()) {
                   throw new EmptyException("顺序表为空！！！");
               }
               int pos = indexOf(key);
               if (pos == -1) {
                   return;
               }
               for (int i = pos; i < this.usedSize - 1; i++) {
                   elem[i] = elem[i + 1];
               }
               this.usedSize--;
           }catch (EmptyException e) {
               e.printStackTrace();
           }
    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void clear() {
        for (int i = 0; i < this.usedSize; i++) {
            elem[i] = null;
        }
        this.usedSize = 0;
    }
}
